﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Evan
{
    /// <summary>
    /// 图片压缩
    /// </summary>
    public class ImageCompressionHelper
    {
        /// <summary>
        /// 将图片控制指定最大宽度等比例缩放
        /// </summary>
        /// <param name="imgSource"></param>
        /// <returns></returns>
        public static Bitmap ImagePercent(Image imgSource, int imgwidth)
        {
            int newW = imgSource.Width < imgwidth ? imgSource.Width : imgwidth;
            int newH = int.Parse(Math.Round(imgSource.Height * (double)newW / imgSource.Width).ToString());
            try
            {
                Bitmap b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
                g.DrawImage(imgSource, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, imgSource.Width, imgSource.Height), GraphicsUnit.Pixel);
                g.Dispose();
                return b;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// JPEG图片质量压缩,输出到硬盘
        /// </summary>
        /// <param name="imgSource"></param>
        /// <param name="outPath"></param>
        /// <param name="flag">压缩比例:取值1到100,越小压缩比越大</param>
        /// <returns></returns>
        public static bool ImageCompression(Image imgSource, string outPath, int flag)
        {
            ImageFormat tFormat = imgSource.RawFormat;
            EncoderParameters ep = new EncoderParameters();
            long[] qy = new long[1];
            qy[0] = flag;
            EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
            ep.Param[0] = eParam;
            try
            {
                ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageDecoders();
                ImageCodecInfo jpegICIinfo = null;
                for (int x = 0; x < arrayICI.Length; x++)
                {
                    if (arrayICI[x].FormatDescription.Equals("JPEG"))
                    {
                        jpegICIinfo = arrayICI[x];
                        break;
                    }
                }
                if (jpegICIinfo != null)
                {
                    imgSource.Save(outPath, jpegICIinfo, ep);
                }
                else
                {
                    imgSource.Save(outPath, tFormat);
                }
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                imgSource.Dispose();
            }

        }

        public static Image ImageCompression(Image imgSource, int flag)
        {
            ImageFormat tFormat = imgSource.RawFormat;
            EncoderParameters ep = new EncoderParameters();
            long[] qy = new long[1];
            qy[0] = flag;
            EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
            ep.Param[0] = eParam;
            try
            {
                ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageDecoders();
                ImageCodecInfo jpegICIinfo = null;
                for (int x = 0; x < arrayICI.Length; x++)
                {
                    if (arrayICI[x].FormatDescription.Equals("JPEG"))
                    {
                        jpegICIinfo = arrayICI[x];
                        break;
                    }
                }
                MemoryStream ms = new MemoryStream();
                if (jpegICIinfo != null)
                {                    
                    imgSource.Save(ms, jpegICIinfo, ep);
                }
                else
                {
                    imgSource.Save(ms, tFormat);
                }
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                Image imgNow = ImageConvertHelper.ByteToImage(arr);
                return imgNow;
            }
            catch
            {
                return imgSource;
            }
            finally
            {
                imgSource.Dispose();
            }
        }
    }
}
